package com.wisezone.workflow.dao.ibatis;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.wisezone.common.dao.impl.BaseDaoImpl;
import com.wisezone.workflow.bean.WfProcessDefinition;
import com.wisezone.workflow.bean.WfVariable;
import com.wisezone.workflow.bean.search.WfVariableSearch;
import com.wisezone.workflow.dao.WfVariableDao;
import com.wisezone.workflow.exception.WfDaoException;
import com.wisezone.workflow.utils.ProcessUtils;

/**
 * IBATIS 变量实现类
 * 
 * @author tss 2009-09-05
 * 
 */
public class WfVariableDaoImpl extends BaseDaoImpl implements WfVariableDao {

	public WfVariableDaoImpl() {
		super();
	}

	/**
	 * 获得流程变量
	 * 
	 * @param processDefId
	 * @return
	 * @throws WfDaoException
	 */
	public List<WfVariable> getProcessVariableList(Long processDefId)
			throws WfDaoException {

		WfVariableSearch searchEntity = new WfVariableSearch();
		searchEntity.setProcessDefId(processDefId);
		searchEntity.setSqlClause("activity_id is null");
		List<WfVariable> list = this.listAll(searchEntity);
		return list;
	}

	/**
	 * 获得活动变量
	 * 
	 * @param activityId
	 * @return
	 * @throws WfDaoException
	 */
	public List<WfVariable> getActivityVariableList(Long activityId)
			throws WfDaoException {

		WfVariableSearch searchEntity = new WfVariableSearch();
		searchEntity.setActivityId(activityId);
		List<WfVariable> list = this.listAll(searchEntity);
		return list;
	}

	/**
	 * 获得流程模板变量
	 * 
	 * @param processDefId
	 * @param name
	 * @return
	 * @throws WfDaoException
	 */
	public WfVariable getProcessEntity(Long processDefId, String name)
			throws WfDaoException {

		WfVariableSearch searchEntity = new WfVariableSearch();
		searchEntity.setProcessDefId(processDefId);
		searchEntity.setName(name);
		searchEntity.addSqlClause("activity_id is null");
		WfVariable entity = this.get(searchEntity);
		return entity;
	}

	/**
	 * 获得活动变量
	 * 
	 * @param s
	 * @param aiId
	 * @return
	 * @throws WfDaoException
	 */
	public WfVariable getActivityEntity(Long activityId, String name)
			throws WfDaoException {

		WfVariableSearch searchEntity = new WfVariableSearch();
		searchEntity.setActivityId(activityId);
		searchEntity.setName(name);
		WfVariable entity = this.get(searchEntity);
		return entity;
	}

	/**
	 * 保存或修改流程变量
	 * 
	 * @param variable
	 * @param processDefinition
	 * @throws WfDaoException
	 */
	public void saveOrUpdate(WfVariable variable,
			WfProcessDefinition processDefinition) throws WfDaoException {

		if (variable == null) {
			throw new IllegalArgumentException("variable 不能为空");
		}
		if (processDefinition == null) {
			throw new IllegalArgumentException("processDefinition 不能为空");
		}

		// 保存或修改流程变量
		this.saveOrUpdate(variable);

		// 同步json 保存
		String fileContent = processDefinition.getFileContent();
		JSONObject jsonObject = new JSONObject(fileContent);
		JSONObject templateForm = jsonObject.getJSONObject("form");
		if (variable.getActivityId() == null) {
			if (!templateForm.has("variableMap")) {
				return;
			}
			JSONArray jsonArray = templateForm.getJSONArray("variableMap");
			ProcessUtils.setVarJson(jsonArray, variable, "saveOrUpdate");
		} else {
			JSONArray jsonArray = templateForm.getJSONArray("nodeArray");
			if (jsonArray == null) {
				return;
			}
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject jsonObj = jsonArray.getJSONObject(i);
				JSONObject jsonForm = jsonObj.getJSONObject("form");
				if (!jsonForm.has("variableMap")) {
					return;
				}
				JSONArray jsonVarMapArray = jsonForm
						.getJSONArray("variableMap");
				ProcessUtils.setVarJson(jsonVarMapArray, variable,
						"saveOrUpdate");
			}
		}

	}

	/**
	 * 删除流程变量
	 * 
	 * @param variable
	 * @param processDefinition
	 * @throws WfDaoException
	 */
	public void delete(WfVariable variable,
			WfProcessDefinition processDefinition) throws WfDaoException {

		if (variable == null) {
			throw new IllegalArgumentException("variable 不能为空");
		}
		if (processDefinition == null) {
			throw new IllegalArgumentException("processDefinition 不能为空");
		}

		// 删除流程变量
		this.delete(variable);

		// 同步json 保存
		String fileContent = processDefinition.getFileContent();
		JSONObject jsonObject = new JSONObject(fileContent);
		JSONObject templateForm = jsonObject.getJSONObject("form");
		if (variable.getActivityId() == null) {
			if (!templateForm.has("variableMap")) {
				return;
			}
			JSONArray jsonArray = templateForm.getJSONArray("variableMap");
			ProcessUtils.setVarJson(jsonArray, variable, "delete");
		} else {
			JSONArray jsonArray = templateForm.getJSONArray("nodeArray");
			if (jsonArray == null) {
				return;
			}
			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject jsonObj = jsonArray.getJSONObject(i);
				JSONObject jsonForm = jsonObj.getJSONObject("form");
				if (!jsonForm.has("variableMap")) {
					return;
				}
				JSONArray jsonVarMapArray = jsonForm
						.getJSONArray("variableMap");
				ProcessUtils.setVarJson(jsonVarMapArray, variable, "delete");
			}
		}

	}

	/**
	 * 根据流程模板Id删除
	 * 
	 * @param id
	 * @throws WfDaoException
	 */
	public void deleteByProcessDefId(Long id) throws WfDaoException {
		this.execute("deleteByProcessDef", id);
	}

}